package com.dodoke.ojserver.service.impl;

import com.dodoke.ojserver.bean.Problem;
import com.dodoke.ojserver.bean.dto.ProblemAddDTO;
import com.dodoke.ojserver.bean.mapper.ProblemMapStruct;
import com.dodoke.ojserver.bean.vo.ProblemVO;
import com.dodoke.ojserver.mapper.ProblemMapper;
import com.dodoke.ojserver.service.IProblemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ProblemServiceImpl implements IProblemService {

    @Autowired
    private ProblemMapper problemMapper;

    @Override
    public int addProblem(ProblemAddDTO problemAddDTO) {
        Problem problem = ProblemMapStruct.PROBLEM_MAPPER.problemAddDtoToProblem(problemAddDTO);
        // 设置问题创建时间
        String current = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format( new Date());
        Timestamp time = Timestamp.valueOf(current);
        problem.setInDate(time);
        return problemMapper.addProblem(problem);
    }

    @Override
    public int deleteProblem(int problemId) {
        return problemMapper.deleteProblem(problemId);
    }

    @Override
    public int deleteProblemList(int[] problemIds) {
        return problemMapper.deleteProblemList(problemIds);
    }

    @Override
    public int editProblem(ProblemAddDTO problemAddDTO) {
        Problem problem = ProblemMapStruct.PROBLEM_MAPPER.problemAddDtoToProblem(problemAddDTO);
        // 设置问题更新时间
        String current = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format( new Date());
        Timestamp time = Timestamp.valueOf(current);
        problem.setInDate(time);
        return problemMapper.editProblem(problem);
    }

    @Override
    public Problem getProblemById(int problemId) {
        return problemMapper.getProblemById(problemId);
    }

    @Override
    public PageInfo<ProblemVO> listProblem(Problem problem, int pageNum, int pageSize, String orderBy) {
        // 开启分页
        PageHelper.startPage(pageNum, pageSize, orderBy);
        // 从数据库中查询出
        List<Problem> problemList = problemMapper.listProblem(problem);
        // 获取分页信息
        PageInfo problemPageInfo = new PageInfo<>(problemList);
        // 创建需要分页的VoList
        List<ProblemVO> problemVOList = new ArrayList<>();
        // 对象转换
        for (Problem p : problemList) {
            ProblemVO problemVO = ProblemMapStruct.PROBLEM_MAPPER.problemToProblemVO(p);
            problemVOList.add(problemVO);
        }
        // 将封装后的列表放到分页对象中
        problemPageInfo.setList(problemVOList);
        return problemPageInfo;
    }

    @Override
    public Problem getProblemRand() {
        return problemMapper.getProblemRand();
    }

    @Override
    public int updateProblemSolvedById(int problemId) {
        return problemMapper.updateProblemSolvedById(problemId);
    }
}
